/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Processing;

import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;

/**
 *
 */
public abstract class Board
{
    protected byte[] m_Board;
    protected Vector m_MoveList;
    protected int MovablePawn;
    protected Hashtable[] currentPawnList;
    protected Vector[] wonPawnList;

    public byte[] GetBoard()
    {
        return m_Board;
    }

    public void SetBoard(byte[] value)
    {
        m_Board = value;
    }

    public Vector GetTurnList()
    {
        return m_MoveList;
    }

    public void SetTurnList(Vector value)
    {
        m_MoveList = value;
    }

    public abstract void InitBoard(int qcDiTruoc);
    protected abstract boolean PlaceAPawn(int vitri, int qCo);
    protected abstract boolean PlaceAPawn(int vitriHang, int vitriCot, int qCo);
    public abstract Vector MovableTurnList(int vitriHang, int vitriCot);
    public abstract Vector MovableList(int vitri);
    public abstract int[] CompMove();
    public abstract boolean WinningCheck(int loaiCo);
    public abstract boolean Win();
    public abstract int[] PlaceAMove(int vtDau, int vtCuoi, int loaiPhong);
    public abstract int[] PlaceAMove(int vtDau, int vtCuoi);
    public abstract int isPawn(int qc);
    public abstract int isKing(int qc);
    public abstract void LoadBoard(byte[] dl);

    public void PawnList(Hashtable pawnList)
    {
        for (int i = 0; i < 2; ++i)
        {
            Enumeration en = currentPawnList[i].keys();
            while(en.hasMoreElements())
            {
                Integer each = (Integer)en.nextElement();
                pawnList.put(each, currentPawnList[i].get(each));
            }
        }
    }

    public void SkipAMove()
    {
        MovablePawn = (MovablePawn == 0) ? 1 : 0;
    }

    protected int GetTotalKillablePawn()
    {
        int result = 0;
        Enumeration en = currentPawnList[MovablePawn].keys();
        while(en.hasMoreElements())
        {
            Integer each = (Integer)en.nextElement();
            Vector temp = MovableList(each.intValue());
            result += ((Integer)temp.elementAt(temp.size() - 1)).intValue();
        }
        return result;
    }

    public int MaxUndoTurns()
    {
        return m_MoveList.size();
    }

    public int[] UndoAMove()
    {
        if (m_MoveList.size() == 0)
            return null;

        int[] result = new int[5];
        String strnc = (String)m_MoveList.elementAt(m_MoveList.size() - 1);
        m_MoveList.removeElementAt(m_MoveList.size() - 1);
        
        String[] cat1 = Util.Split(strnc, ";");
        String[] cat2 = Util.Split(cat1[0], "_");
        String[] cat3 = Util.Split(cat1[1], "_");

        int startPos = Integer.parseInt(cat2[0]);
        int startPawn = Integer.parseInt(cat2[1]);
        int pawn = Integer.parseInt(cat2[2]);

        int destPos = Integer.parseInt(cat3[0]);
        int takenPawn = Integer.parseInt(cat3[1]);

        PlaceAPawn(destPos, takenPawn);
        PlaceAPawn(startPos, startPawn);
        MovablePawn = (MovablePawn == 0) ? 1 : 0;
        if (takenPawn != 0)
        {
            wonPawnList[MovablePawn].removeElementAt(wonPawnList[MovablePawn].size() - 1);
            currentPawnList[(MovablePawn == 0) ? 1 : 0].put(new Integer(destPos), new Integer(takenPawn));
        }
        currentPawnList[MovablePawn].remove(new Integer(destPos));
        currentPawnList[MovablePawn].put(new Integer(startPos), new Integer(startPawn));

        result[0] = startPos;
        result[1] = startPawn;
        result[2] = pawn;
        result[3] = destPos;
        result[4] = takenPawn;
        
        return result;
    }

    public byte[] ExportBoard()
    {
        int export = (currentPawnList[0].size() + currentPawnList[1].size()) *2 + wonPawnList[0].size() + wonPawnList[1].size() + 5;
        byte[] result = new byte[export];
        int cs = 0;
        for (int i = 0; i < 2; ++i )
        {
            Enumeration en = currentPawnList[i].keys();
            while (en.hasMoreElements())
            {
                Integer each = (Integer)en.nextElement();
                result[++cs] = each.byteValue();
                result[++cs] = ((Integer)currentPawnList[i].get(each)).byteValue();
            }
            result[++cs] = (byte)0xff;
            
            for(int j=0; j< wonPawnList[i].size(); ++j)
            {
                result[++cs] = ((Integer)wonPawnList[i].elementAt(j)).byteValue();
            }
            result[++cs] = (byte)0xff;
        }

        return result;
    }
}
